﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using RTSoft.Modules;
using System.Data;

namespace RTSoft.SoureCode.Control.DonGiaBan
{
    class dongia_banctrl
    {
        DataProvider provider = new DataProvider();
        SqlCommand cmd = new SqlCommand();
        SqlDataReader reader;
        String strSQL = "";

        public void getData(ref String errConnect, ref String errSQL, ref DataTable tbl)
        {
            provider.Connection(ref errConnect);
            try
            {
                tbl = new DataTable();
                strSQL = "select ID_MATHANG,DONGIA,ID_NGAY from DONGIA_BAN ";
                tbl = provider.LayDanhSach(strSQL);
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
        }

        public int getPrice(ref String errConnect, ref String errSQL, int ID_MATHANG)
        {
            int price = 0;
            try
            {
                cmd.Connection = provider.Connection(ref errConnect);
                strSQL = "select DONGIA from DONGIA_BAN WHERE ID_MATHANG = @ID_MATHANG";
                cmd.CommandText = strSQL;
                cmd.Parameters.AddWithValue("@ID_MATHANG", ID_MATHANG);
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    price = Convert.ToInt32(reader[0]);
                }
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
            return price;
        }

     
        public int checkExist(ref String errConnect, ref String errSQL, int id)
        {
            int rs = 0;
            cmd.Connection = provider.Connection(ref errConnect);
            try
            {
                strSQL = "select * from DONGIA_BAN where id_mathang = " + id;
                cmd.CommandText = strSQL;
                rs = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                return 0;
            }
            finally
            {
                provider.Disconnect();
            }
            return rs;
        }

        public void Insert(ref String errConnect, ref String errSQL, dongia_baninfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Insert into DONGIA_BAN";
                strSQL = strSQL + "(ID_MATHANG,ID_NGAY,DONGIA)";
                strSQL = strSQL + " values";
                strSQL = strSQL + "(@ID_MATHANG,@ID_NGAY,@DONGIA)";
                cmd.CommandText = strSQL;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ID_MATHANG", info.ID_MATHANG);
                cmd.Parameters.AddWithValue("@ID_NGAY", info.ID_NGAY);
                cmd.Parameters.AddWithValue("@DONGIA", info.DONGIA);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Update(ref String errConnect, ref String errSQL, dongia_baninfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Update DONGIA_BAN SET  ";
                strSQL = strSQL + "ID_NGAY=@ID_NGAY,DONGIA=@DONGIA Where  ID_MATHANG = @ID_MATHANG";
                cmd.CommandText = strSQL;

                cmd.Parameters.AddWithValue("@ID_MATHANG", info.ID_MATHANG);
                cmd.Parameters.AddWithValue("@ID_NGAY", info.ID_NGAY);
                cmd.Parameters.AddWithValue("@DONGIA", info.DONGIA);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Delete(ref String errConnect, ref String errSQL, int id)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                strSQL = "DELETE FROM DONGIA_BAN WHERE ID_MATHANG=@RowID";
                cmd.Parameters.AddWithValue("@RowID", id);
                cmd.CommandText = strSQL;
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
                //' Thuc hien transaction 
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
        }
    }
}
